<?php
namespace app\farm\model;

use app\BaseModel;
use think\facade\Db;

class Farmer extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_farmer';


    protected $append = [

        'time_status'
    ];


    /**
     * @author chenniang
     * @DataTime: 2022-03-02 17:12
     * @功能说明:
     */
    public function getTimeStatusAttr($value,$data){

        if(!empty($data['start_time'])&&!empty($data['end_time'])){

            $start_time = strtotime($data['start_time']);

            $end_time   = strtotime($data['end_time']);

            if($start_time<time()&&$end_time>time()){

                return 1;
            }else{

                return 0;
            }

        }


    }

    /**
     * @author chenniang
     * @DataTime: 2021-12-30 11:22
     * @功能说明:
     */
    public function getImgsAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);
        }

    }

    /**
     * @author chenniang
     * @DataTime: 2021-12-30 11:22
     * @功能说明:
     */
    public function getIdcardImgsAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);
        }

    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataDistanceList($dis,$alh,$alhs=[],$page=10){

        $data = $this->where($dis)->where($alhs)->field(['*',$alh])->order('distance asc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-30 11:26
     * @功能说明:后台列表
     */
    public function adminDataList($dis,$page=10,$where=[]){

        $data = $this->alias('a')
                ->join('lbfarm_user_list b','a.user_id = b.id','left')
                ->where($dis)
                ->where(function ($query) use ($where){
                    $query->whereOr($where);
                })
                ->field('a.*,b.nickName,b.avatarUrl')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis,$file='*'){

        $data = $this->where($dis)->field($file)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-22 11:30
     * @功能说明:检查用户是否是地主
     */
    public function landlordCheck($user_id){

        $cap_dis[] = ['user_id','=',$user_id];

        $cap_dis[] = ['type','=',1];

        $cap_dis[] = ['status','in',[2,3]];

        $farmer_model = new \app\farm\model\Farmer();
        //查看是否是团长
        $cap_info = $farmer_model->dataInfo($cap_dis);

        $is_landlord = 1;

        if(empty($cap_info)){

            $order_model = new LandOrder();

            $where[] = ['pay_type','>',1];

            $where[] = ['user_id','=',$user_id];
            //是否有土地订单
            $order = $order_model->dataInfo($where);

            if(empty($order)){

                $is_landlord = 0;

            }

        }

        $user_model = new User();

        $user_model->dataUpdate(['id'=>$user_id],['is_landlord'=>$is_landlord]);

        return $is_landlord;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-22 11:53
     * @功能说明:初始化地主表
     */
    public function initLandLord($data,$uniacid){

        $dis = [

            'type'    => 2,

            'user_id' => $data['user_id'],

            'uniacid' => $uniacid
        ];

        $find = $this->dataInfo($dis);

        if(empty($find)){

            $dis['status'] = 2;

            $dis['title'] = $data['title'];

            $dis['cover'] = $data['cover'];

            $dis['address'] = $data['address'];

            $dis['lat'] = $data['lat'];

            $dis['lng'] = $data['lng'];

            $dis['desc'] = $data['desc'];

            $dis['mobile'] = $data['mobile'];

            $this->dataAdd($dis);

            $user_model = new User();

            $user_model->dataUpdate(['id'=>$data['user_id']],['is_landlord'=>1]);
        }

        return true;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-21 15:21
     * @功能说明:保留两位小数
     */
    public function getDistanceAttr($value){

        if(!empty($value)){

            if($value>1000){

                $value = $value/1000;

                $value =  round($value,2);

                $value = $value.'km';
            }else{

                $value =  round($value,2);

                $value = $value.'m';

            }

            return $value;

        }

    }




    /**
     * @author chenniang
     * @DataTime: 2022-02-28 14:08
     * @功能说明:添加商品时农场主下拉框
     */
    public function goodsFarmerSelect($user_id){

        $land_order_model = new LandOrder();

        $dis[] = ['user_id','=',$user_id];

        $dis[] = ['pay_type','>',1];
        //购买土地对农场主id
        $order_farmer = $land_order_model->where($dis)->column('farmer_id');

        $where[] = ['user_id','=',$user_id];

        $where[] = ['status','in',[2,3]];
        //如果自己是农场主
        $farmer = $this->where($where)->column('id');

        $order_farmer = array_merge($order_farmer,$farmer);

        return $order_farmer;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-28 16:06
     * @功能说明:
     */
    public function farmerId($uniacid){

        $dis = [

            'status' => 2,

            'uniacid'=> $uniacid
        ];

        $id = $this->where($dis)->column('id');

        return $id;

    }





}